
%% == 5AP Burst ==

colWidCM = [85, 114, 174]/10; % in cm

dpath = '/Users/landauland/Documents/Research/SabatiniLab/data/manuscriptPreparation/compileData';
f3resAB = load(fullfile(dpath,'figure3resultsAB'));
res = f3resAB.res;

tv = 1:512; 

distance = [res(:).distance];
distidx = distance>80;
dap = cell2mat(cellfun(@(c) c(1:512), {res(:).dap}, 'uni', 0));
dburst = cell2mat(cellfun(@(c) c(1:512), {res(:).dburst}, 'uni', 0));
dpk = cell2mat({res(:).dpk}');

minAllowed = 0.001;
dpk(dpk<minAllowed,1)=minAllowed;

idxSilent = (dpk(:,1) <= 0.04) & distidx(:);
idxActive = (dpk(:,1) >= 0.1) & (dpk(:,1) <= 0.3) & distidx(:);

baseap = mean(dap(80:98,:),1);
baseburst = mean(dburst(80:98,:),1);
bap = dap - baseap;
burst = dburst - baseburst;

fontSize = 7;
lineWidth = 0.5;
markerSize = 4;


% Figure 3A
axAspectRatio = 10.5/13;
axWidth =colWidCM(3)/4;
axHeight= colWidCM(3)/4*axAspectRatio;
xShow = 50:399;
figure(1); clf; hold on;
set(gcf,'units','centimeters','position',[40 30 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.1 0.1 axWidth*0.95 axHeight*0.95]);
shadedErrorBar(xShow, mean(bap(xShow,idxActive),2),std(bap(xShow,idxActive),1,2)/sqrt(sum(idxActive)),{'color','k','linewidth',lineWidth},0);
shadedErrorBar(xShow, mean(bap(xShow,idxSilent),2),std(bap(xShow,idxSilent),1,2)/sqrt(sum(idxSilent)),{'color','b','linewidth',lineWidth},0);
shadedErrorBar(xShow, mean(burst(xShow,idxActive),2),std(burst(xShow,idxActive),1,2)/sqrt(sum(idxActive)),{'color','k','linewidth',lineWidth},0);
shadedErrorBar(xShow, mean(burst(xShow,idxSilent),2),std(burst(xShow,idxSilent),1,2)/sqrt(sum(idxSilent)),{'color','b','linewidth',lineWidth},0);
xlim([75 400]);
ylim([-0.1 1]);
line([100 100],[-0.1 -0.065],'color','k','linewidth',lineWidth);
burstTimes = 100:(1000/150):100+4*1000/150;
for bt = burstTimes
    line([bt bt],[-0.055 -0.02],'color','k','linewidth',lineWidth);
end
line([175 175],[0.325 0.425],'color','k','linewidth',lineWidth);
line([175 225],[0.325 0.325],'color','k','linewidth',lineWidth);
text(158,0.325,'10% \DeltaG/R','Fontsize',fontSize,'HorizontalAlignment','Left','Rotation',90);
text(150,0.325,'40 mV','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
text(175,0.325,'50/100 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
set(gca,'visible','off');
text(160,0.925,'5APs, 150 Hz','Fontsize',fontSize,'HorizontalAlignment','Left');


% Figure 3B
% Setback axes labels in units of the respective axes...
setBackX = [-0.06 -0.099 -0.099 -0.2390];
setBackY = [-0.015 -0.023 -0.027 -0.069];
figure(2); clf; hold on;
set(gcf,'units','centimeters','position',[40 20 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.8 0.68 axWidth*0.775 axHeight*0.785]);
patch([0 0.04 0.04 0],[0 0 1.7 1.7],[0.5 0.5 1],'EdgeColor','none');
patch([0.1 0.3 0.3 0.1],[0 0 1.7 1.7],[0.5 0.5 0.5],'EdgeColor','none');
plot([0 0.45],6*[0 0.45],'color','k','linewidth',lineWidth);
plot(dpk(distidx,1), dpk(distidx,2), 'color','k','marker','.','markersize',markerSize,'linestyle','none');
xlim([setBackY(2) 0.45]);
ylim([setBackX(2) 1.7]);
line([0 0.45],setBackX(1)+[0,0],'color','k','linewidth',lineWidth);
line(setBackY(1)+[0 0],[0 1.7],'color','k','linewidth',lineWidth);
xticks = 0:0.2:0.4;
for xt = xticks
    line(xt+[0,0],setBackX([1 2]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'fontsize',fontSize,'horizontalalignment','center','verticalAlignment','top');
end
text(0.225,setBackX(4),'\DeltaCa_{AP} (\DeltaG/R)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
yticks = 0:0.5:1.5;
for yt = yticks
    line(setBackY([1 2]),yt+[0,0],'color','k','linewidth',lineWidth);
    text(setBackY(3),yt,num2str(yt),'fontsize',fontSize,'horizontalalignment','right');
end
text(setBackY(4),1.7/2,'\DeltaCa_{5APs} (\DeltaG/R)','fontsize',fontSize,'HorizontalAlignment','center','verticalAlignment','bottom','Rotation',90);
set(gca,'visible','off');



%% == 4AP ==

dpath = '/Users/landauland/Documents/Research/SabatiniLab/data/manuscriptPreparation/compileData';
f3resCD = load(fullfile(dpath,'figure3resultsCD'));
res = f3resCD.res;


cnum = [res(:).cellNum];
distance = [res(:).distance]; 

tvec = 1:512;
ptv = 0.1:0.1:1000;

dbase = cell2mat({res(:).dbase}');
dap = cell2mat({res(:).dap})-dbase(:,1)';
d5ap = cell2mat({res(:).d5ap})-dbase(:,2)';
dap4AP = cell2mat({res(:).dap4AP})-dbase(:,3)';
d5ap4AP = cell2mat({res(:).d5ap4AP})-dbase(:,4)';

dpk = cell2mat({res(:).dpk}'); 
dpk(dpk<0)=0; 

pap = cell2mat({res(:).pap});
pap_5ap = cell2mat({res(:).p5ap});
pap_4AP = cell2mat({res(:).pap4AP});
pap_5ap4AP = cell2mat({res(:).p5ap4AP});

restTime = find(ptv>=96,1);
rest = cat(2, pap(restTime,:)', pap_4AP(restTime,:)');
restDiff = rest(:,2) - rest(:,1);
adpTime = find(ptv>=106.5,1);
adp = cat(2, pap(adpTime,:)', pap_4AP(adpTime,:)') - rest;
adpDiff = adp(:,2) - adp(:,1); 

gcol = [0 0.65 0];
distidx = distance>80;
NR = sum(distidx);
divFactor = sqrt(NR);
xShow = 50:399;

idxSilent = dpk(:,1)<=0.04 & distidx(:); 
idxActive = dpk(:,1)>=0.1 & dpk(:,1)<=0.3 & distidx(:);

% Figure 2C
figure(3); clf; hold on;
set(gcf,'units','centimeters','position',[45 30 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.1 0.1 axWidth*0.95 axHeight*0.95]);
shadedErrorBar(xShow, nanmean(dap(xShow,idxActive),2),nanstd(dap(xShow,idxActive),1,2)/sqrt(sum(idxActive)),{'color','k','linewidth',lineWidth},0);
shadedErrorBar(xShow, nanmean(dap(xShow,idxSilent),2),nanstd(dap(xShow,idxSilent),1,2)/sqrt(sum(idxSilent)),{'color','b','linewidth',lineWidth},0);
shadedErrorBar(xShow, nanmean(dap4AP(xShow,idxActive),2),nanstd(dap4AP(xShow,idxActive),1,2)/sqrt(sum(idxActive)),{'color','k','linewidth',lineWidth},0);
shadedErrorBar(xShow, nanmean(dap4AP(xShow,idxSilent),2),nanstd(dap4AP(xShow,idxSilent),1,2)/sqrt(sum(idxSilent)),{'color','b','linewidth',lineWidth},0);
xlim([75 400]);
ylim([-0.07 1.25]);
line([100 100],[-0.07 -0.03],'color','k','linewidth',lineWidth);
text(145,1.125,'in 4-AP','Fontsize',fontSize,'HorizontalAlignment','Left');
line([150 150],[0.3 0.4],'color','k','linewidth',lineWidth);
line([150 180],[0.3 0.3],'color','k','linewidth',lineWidth);
text(150,0.3,'10% \DeltaG/R','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
text(132,0.3,'20 mV','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
text(150,0.3,'30/75 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
pTvIdx = find(ptv>=96,1):find(ptv>=132,1);
pTvUse = ptv(pTvIdx);
pTvTrans = @(tv) tv*2.5 + 45;
pVTrans = @(v) 0.005*(v+80)+0.55;
insetPap4AP = pVTrans(pap_4AP(pTvIdx,distidx));
insetPap = pVTrans(pap(pTvIdx,distidx));
shadedErrorBar(pTvTrans(pTvUse),nanmean(insetPap4AP,2),nanstd(insetPap4AP,1,2),{'color',gcol,'linewidth',lineWidth},0);
shadedErrorBar(pTvTrans(pTvUse),nanmean(insetPap,2),nanstd(insetPap,1,2),{'color','k','linewidth',lineWidth},0);
line(pTvTrans([100 100]),pVTrans([-94 -87]),'color','k','linewidth',lineWidth);
plot(pTvTrans([107 114]),pVTrans([10 10]),'color',gcol,'linewidth',lineWidth);
text(pTvTrans(116),pVTrans(10),'in 4-AP','Fontsize',fontSize-1,'HorizontalAlignment','Left');
set(gca,'visible','off');


% Figure 2D
% Setback axes labels in units of the respective axes...
setBackX = [-0.06 -0.099 -0.099 -0.2390];
setBackY = [-0.015 -0.023 -0.027 -0.08];
figure(4); clf; hold on;
set(gcf,'units','centimeters','position',[45 20 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.8 0.68 axWidth*0.775 axHeight*0.785]);
patch([0 0.04 0.04 0],[0 0 1.7 1.7],[0.5 0.5 1],'EdgeColor','none');
patch([0.1 0.3 0.3 0.1],[0 0 1.7 1.7],[0.5 0.5 0.5],'EdgeColor','none');
plot(dpk(distidx,1), dpk(distidx,3), 'color','k','marker','.','markersize',markerSize,'linestyle','none');
xlim([setBackY(2) 0.45]);
ylim([setBackX(2) 1.7]);
line([0 0.45],setBackX(1)+[0,0],'color','k','linewidth',lineWidth);
line(setBackY(1)+[0 0],[0 1.7],'color','k','linewidth',lineWidth);
xticks = 0:0.2:0.4;
for xt = xticks
    line(xt+[0,0],setBackX([1 2]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'fontsize',fontSize,'horizontalalignment','center','verticalAlignment','top');
end
text(0.45/2,setBackX(4),'\DeltaCa_{AP} (\DeltaG/R)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
yticks = 0:0.5:1.5;
for yt = yticks
    line(setBackY([1 2]),yt+[0,0],'color','k','linewidth',lineWidth);
    text(setBackY(3),yt,num2str(yt),'fontsize',fontSize,'horizontalalignment','right');
end
text(setBackY(4),1.7/2,'in 4-AP (\DeltaG/R)','fontsize',fontSize,'HorizontalAlignment','center','verticalAlignment','bottom','Rotation',90);
set(gca,'visible','off');





%% == picospritzer ==

colWidCM = [85, 114, 174]/10; % in cm
axAspectRatio = 10.5/13;
axWidth =colWidCM(3)/4;
axHeight= colWidCM(3)/4*axAspectRatio;
fontSize = 7;
lineWidth = 0.5;
markerSize = 4;

dpath = '/Users/landauland/Documents/Research/SabatiniLab/data/manuscriptPreparation/compileData';
f3resEH = load(fullfile(dpath,'figure3resultsEH'));
res = f3resEH.res;
NR = numel(res);

distance = [res(:).distance];
exper = {res(:).exper};
dendrite = [res(:).dendrite];
puff = [res(:).puffSolution];
parLoc = [res(:).parentLocation];

idxControl = puff==0; % Applied ACSF for these (applied glutamate for all others)
idxDistance = distance>80;

idxUse = ~idxControl & idxDistance; 

bswin = [50 90];

% get dendritic imaging
tva = 1:512;
tvp = 1:512*3;
dap = cell2mat({res(:).dap}); dap = dap(1:512, :);
d5ap = cell2mat({res(:).d5aps}); d5ap = d5ap(1:512, :);
dpuff = cell2mat({res(:).dpuff});
dpuffFar = cell2mat({res(:).dpuffFar});
dpuffAll = cellfun(@(c) c{2}, {res(:).ddata}, 'uni', 0);
dpuffBase = cellfun(@(c) mean(c(bswin(1):bswin(2),:),1), dpuffAll,'uni',0);
bpuffAll = cellfun(@(p,b) p-b, dpuffAll,dpuffBase, 'uni', 0);

% get physiology 
ptv = 0.1:0.1:1000; 
pap = cell2mat({res(:).pap}); 
p5aps = cell2mat({res(:).p5aps}); 
ppuff = cell2mat({res(:).ppuff}); 
ppuffFar = cell2mat({res(:).ppuffFar}); 
ptvec = 0.1:0.1:1000; 
pBaseWin = [80 99]; 
pPkWin = [100 120]; 
pbaseSample = [find(ptvec>=pBaseWin(1),1) find(ptvec>=pBaseWin(2),1)]; 
ppkSample = [find(ptvec>=pPkWin(1),1) find(ptvec>=pPkWin(2),1)]; 
pbase = mean(ppuff(pbaseSample(1):pbaseSample(2),:),1); 
ppuffbase = ppuff - pbase; 
ppk = mean(ppuffbase(ppkSample(1):ppkSample(2),:),1); 
 
% -- analyze peaks dendrites --
dapbase = mean(dap(bswin(1):bswin(2),:),1);
d5apbase = mean(d5ap(bswin(1):bswin(2),:),1);
dpuffbase = mean(dpuff(bswin(1):bswin(2),:),1);
dpuffFarbase = mean(dpuffFar(bswin(1):bswin(2),:),1);
bap = dap - dapbase;
b5ap = d5ap - d5apbase;
bpuff = dpuff - dpuffbase;
bpuffFar = dpuffFar - dpuffFarbase;

dpk_ap = nanmean(bap(107:117,:),1);
dpk_5ap = nanmean(b5ap(133:143,:),1);
[~,idx] = max(smoothsmooth(bpuff(100:end,:),20),[],1);
idx = idx + 100 - 1;
dpk_puff = zeros(1,NR);
for nr = 1:NR
    if idx(nr)+10>1536
        idx(nr) = 1536-10;
    end
    dpk_puff(nr) = mean(bpuff(idx(nr):idx(nr)+10,nr),1);
end

idxCell = num2cell(idx);
bpuffPk = cellfun(@(puff,pkidx) nanpad(mean(puff(pkidx:pkidx+10,:),1),9), bpuffAll,idxCell, 'uni', 0);
bpuffPk = cell2mat(cellfun(@(c) c(:)', bpuffPk, 'uni', 0)');



[~,idx] = max(smoothsmooth(bpuffFar(100:end,:),20),[],1);
idx = idx + 100 - 1;
dpk_puffFar = zeros(1,NR);
for nr = 1:NR
    if idx(nr)+10>1536
        idx(nr) = 1536-10;
    end
    dpk_puffFar(nr) = mean(bpuffFar(idx(nr):idx(nr)+10,nr),1);
end
dpk = cat(2, dpk_ap', dpk_5ap', dpk_puff', dpk_puffFar');
dpk(dpk<0)=0;


% -- Figure 3F --
idxExample = 17;
idxChoices = find(idxUse);
exidx = idxChoices(idxExample);
exdap = res(exidx).ddata{1};
ex5ap = res(exidx).ddata{3};
expuff = res(exidx).ddata{2};
exdap = exdap - mean(exdap(80:98,1));
ex5ap = ex5ap - mean(ex5ap(80:98,1));
expuff = expuff - mean(expuff(80:98,1));
xShow = 50:400;
figure(6); clf; hold on;
set(gcf,'units','centimeters','position',[50 20 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.1 0.1 axWidth*0.95 axHeight*0.95]);
shadedErrorBar(xShow, mean(exdap(xShow,:),2),std(exdap(xShow,:),1,2)/sqrt(size(exdap,2)),{'color','k','linewidth',lineWidth},0);
shadedErrorBar(xShow, mean(ex5ap(xShow,:),2),std(ex5ap(xShow,:),1,2)/sqrt(size(ex5ap,2)),{'color',[0.5,0,0],'linewidth',lineWidth},0);
shadedErrorBar(xShow, mean(expuff(xShow,:),2),std(expuff(xShow,:),1,2)/sqrt(size(expuff,2)),{'color','r','linewidth',lineWidth},0);
xlim([40 410]);
ylim([-0.31 2.3]);
% Draw ticks for AP Times
for xt = 100:(1000/150):(100+4*1000/150)
    line([xt,xt],[-0.20 -0.11],'color','k','linewidth',lineWidth);
end
line([100 100],[-0.31 -0.22],'color','k','linewidth',lineWidth);
% Scale Bar
line([325 325],[0.6 0.9],'color','k','linewidth',lineWidth);
line([325 375],[0.6 0.6],'color','k','linewidth',lineWidth);
text(325,0.6,'50 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(325,0.6,'30% \DeltaG/R','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
% Make Legend
line([45 70],2.1*[1,1],'color','k','linewidth',lineWidth);
line([45 70],1.85*[1,1],'color',[0.5,0,0],'linewidth',lineWidth);
line([45 70],1.6*[1,1],'color','r','linewidth',lineWidth);
text(78,2.1,'1 AP','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Middle');
text(78,1.85,'5 APs','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Middle');
text(78,1.6,'Glu Puff','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Middle');
set(gca,'visible','off');


% -- Figure 3G --
xShow = 50:450;
numGlu = sum(idxUse);
figure(7); clf; hold on;
set(gcf,'units','centimeters','position',[55 30 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.1 0.1 axWidth*0.95 axHeight*0.95]);
shadedErrorBar(xShow, mean(bap(xShow,idxUse),2),std(bap(xShow,idxUse),1,2)/sqrt(numGlu),{'color','k','linewidth',lineWidth},0);
shadedErrorBar(xShow, nanmean(b5ap(xShow,idxUse),2),nanstd(b5ap(xShow,idxUse),1,2)/sqrt(numGlu),{'color',[0.5,0,0],'linewidth',lineWidth},0);
shadedErrorBar(xShow, mean(bpuff(xShow,idxUse),2),std(bpuff(xShow,idxUse),1,2)/sqrt(numGlu),{'color','r','linewidth',lineWidth},0);
xlim([40 460]);
ylim([-0.3 3.1]);
% Draw ticks for AP times
for xt = 100:(1000/150):(100+4*1000/150)
    line([xt,xt],[-0.17 -0.06],'color','k','linewidth',lineWidth);
end
line([100 100],[-0.3 -0.19],'color','k','linewidth',lineWidth);
% Draw Scale Bar
line([325 325],[0.6 0.9],'color','k','linewidth',lineWidth);
line([325 375],[0.6 0.6],'color','k','linewidth',lineWidth);
text(325,0.6,'50 ms','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Top');
text(323.5,0.6,'30% \DeltaG/R','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');


% -- Figure 3H --
setBackX = [-0.01 -0.015 -0.018 -0.035];
setBackY = [-0.2 -0.3 -0.3 -0.9];
figure(8); clf; hold on;
set(gcf,'units','centimeters','position',[55 20 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.65 0.68 axWidth*0.785 axHeight*0.775]);
plot(dpk(:,1),dpk(:,2),'color',[0.5,0,0],'marker','.','linestyle','none','markersize',markerSize);
plot(dpk(:,1),dpk(:,3),'color','r','marker','.','linestyle','none','markersize',markerSize);

xlim([setBackX(2) 0.3]);
ylim([setBackY(2) 6]);
line([0 0.3],setBackY([1,1]),'color','k','linewidth',lineWidth);
line(setBackX([1,1]),[0 6],'color','k','linewidth',lineWidth);
xticks = 0:0.1:0.3;
for xt = xticks
    line([xt,xt],setBackY([2,1]),'color','k','linewidth',lineWidth);
    text(xt,setBackY(3),num2str(xt),'FOntsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
end
text(0.15,setBackY(4),'\DeltaG/R_{AP} (\DeltaG/R)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
yticks = 0:2:6;
for yt = yticks
    line(setBackX([2,1]),[yt,yt],'color','k','linewidth',lineWidth);
    text(setBackX(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackX(4),3,'\DeltaCa_{5APs, Glu} (\DeltaG/R)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
text(0.235,5.6,'5 APs','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Middle');
text(0.235,5,'Glu Puff','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Middle');
plot(0.225,5.6,'marker','.','markersize',markerSize,'color',[0.5,0,0]);
plot(0.225,5,'marker','.','markersize',markerSize,'color','r');
set(gca,'visible','off');






%% -- Figure 3 - figure supplement 1 --

% Figure 3 - figure supplement 1a
xShow = 50:800;
figure(1); clf; hold on;
set(gcf,'units','centimeters','position',[41 30 axWidth(1) axHeight]);
set(gca,'units','centimeters','position',[0.15 0.15 axWidth(1)*0.925 axWidth(1)*0.8]); % Note difference for equal axes!
shadedErrorBar(tvp(xShow), mean(bpuff((xShow),idxUse),2), std(bpuff((xShow),idxUse),1,2)/sqrt(sum(idxUse)), {'color','b','linewidth',lineWidth},1);
shadedErrorBar(tvp(xShow), mean(bpuff((xShow),idxControl),2), std(bpuff((xShow),idxControl),1,2)/sqrt(sum(~idxControl)), {'color','k','linewidth',lineWidth},1);
xlim([min(xShow) max(xShow)]);
ylim([-0.4 3.2]);
line([100,100],[-0.4 -0.2],'color','k','linewidth',lineWidth);
% Make Scale Bar
legPos = [250 0.5 50 0.25];
line(legPos(1) + [0 legPos(3)],legPos([2,2]),'color','k','linewidth',lineWidth);
line(legPos([1,1]),legPos(2) + [0 legPos(4)],'color','k','linewidth',lineWidth);
text(legPos(1),legPos(2),sprintf('%d ms',legPos(3)),'FontSize',fontSize,'horizontalAlignment','Left','VerticalAlignment','Top');
text(legPos(1),legPos(2),[sprintf('%.2f',legPos(4)),' \DeltaG/R'],'FontSize',fontSize,'horizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
% Make Label
labelPos = [770 -50 -15 2.8 -0.35];
line(labelPos(1) + [0 labelPos(2)],labelPos(4)*[1,1],'color','b','linewidth',lineWidth);
line(labelPos(1) + [0 labelPos(2)],(labelPos(4)+labelPos(5))*[1,1],'color','k','linewidth',lineWidth);
text(sum(labelPos(1:3)),labelPos(4) + 0*labelPos(5), 'Glutamate','Fontsize',fontSize,'HorizontalAlignment','Right');
text(sum(labelPos(1:3)),labelPos(4) + 1*labelPos(5), 'ACSF','Fontsize',fontSize,'HorizontalAlignment','Right');
set(gca,'visible','off');


% Figure 3 - figure supplement 1b
xPos = [0.5, 1];
setBackX = [-0.35 -0.55 -0.5 -0.2];
setBackY = [0 -0.08 -0.12 -0.4];
figure(2); clf; hold on; 
set(gcf,'units','centimeters','position',[46 30 axWidth(1)/3 axHeight]);
set(gca,'units','centimeters','position',[0.6 0.75 axWidth(1)/2*0.7 axHeight*0.7]); % Note difference for equal axes!
plot(xPos(1), dpk_puff(idxUse), 'color','b','marker','.','markersize',markerSize,'linestyle','none');
plot(xPos(2), dpk_puff(idxControl), 'color','k','marker','.','markersize',markerSize,'linestyle','none');
xlim([setBackY(2) 2]);
ylim([setBackX(2) 6]);
line(setBackY([1,1]),[0 6],'color','k','linewidth',lineWidth);
xTicks = xPos;
xlabels = {'Glu','ACSF'};
for xtidx = 1:length(xTicks)
    line(xTicks(xtidx)*[1,1],setBackX([1 2]),'color','k','linewidth',lineWidth);
    text(xTicks(xtidx),setBackX(3),xlabels{xtidx},'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Top','Rotation',45);
end
yticks = 0:2:6;
for yt = yticks
    line(setBackY([1,2]),yt*[1,1],'color','k','linewidth',lineWidth);
    text(setBackY(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackY(4),3,'\DeltaG/R','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off')



% Figure 3 - figure supplement 1C
setBackX = [-8 -12 -12 -29];
setBackY = [0.5 0.28 0.15 -0.85];
plotExample = 28;
nTrial = size(dpuffAll{plotExample},2);
cmap = jet(nTrial);
cmap = cmap*0.7;
intExample = 100*interp1(1:nTrial,bpuffPk(plotExample,1:nTrial)./bpuffPk(plotExample,1),1:0.5:nTrial);
figure(3); clf; hold on;
set(gcf,'units','centimeters','position',[51 30 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.8 0.6 axWidth*0.8 axHeight*0.785]);
plot(1:9, 100*bpuffPk(idxUse,:)./bpuffPk(idxUse,1), 'color','k','marker','.','linewidth',lineWidth/2,'markersize',markerSize);
for nt = 1:nTrial
    plot(nt,100*bpuffPk(plotExample,nt)./bpuffPk(plotExample,1),'color',cmap(nt,:),'marker','.','markersize',markerSize+2);
    if nt<nTrial
        line([nt nt+0.5],intExample(2*(nt-1)+(1:2)),'color',cmap(nt,:),'linewidth',lineWidth+0.2);
    end
    if nt>1
        line([nt-0.5 nt],intExample(2*(nt-1)+(0:1)),'color',cmap(nt,:),'linewidth',lineWidth+0.2);
    end
end
xlim([setBackY(2) 8.5])
ylim([setBackX(2) 175]);
line([1 8],setBackX([1,1]),'color','k','linewidth',lineWidth);
xticks = 1:1:8;
for xt = xticks
    line(xt+[0,0],setBackX([1,2]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'fontsize',fontSize,'horizontalAlignment','Center','VerticalAlignment','Top');
end
text(8.5/2,setBackX(4),'Trial #','Fontsize',fontSize','horizontalAlignment','Center','VerticalAlignment','Top');
line(setBackY([1,1]),[0 175],'color','k','linewidth',lineWidth);
yticks = 0:50:150;
for yt = yticks
    line(setBackY([1,2]),yt+[0,0],'color','k','linewidth',lineWidth);
    text(setBackY(3),yt,num2str(yt),'fontsize',fontSize,'horizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackY(4),175/2,'% \DeltaG/R','Fontsize',fontSize','horizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');

% Figure 3 - figure supplement 1D
figure(4); clf; hold on; 
set(gcf,'units','centimeters','position',[56 30 axWidth(1) axHeight]);
set(gca,'units','centimeters','position',[0.1 0.1 axWidth(1)*0.925 axWidth(1)*0.75]); % Note difference for equal axes!
T = size(dpuffAll{plotExample},1);
xUse = 13:T-13;
offsetShift = 100;
for nt = 1:nTrial
    plot(xUse+offsetShift*(nt-1),bpuffAll{plotExample}(xUse,nt),'color',cmap(nt,:),'linewidth',lineWidth)
    line([100,100]+offsetShift*(nt-1),[-0.25 -0.15],'color',cmap(nt,:),'linewidth',lineWidth);
end
xlim([0 T + offsetShift*(nTrial-1)-500]);
ylim([-0.25 2.5]);

% Make Scale Bar
legPos = [1200 1 100 0.2];
line(legPos(1) + [0 legPos(3)],legPos([2,2]),'color','k','linewidth',lineWidth);
line(legPos([1,1]),legPos(2) + [0 legPos(4)],'color','k','linewidth',lineWidth);
text(legPos(1),legPos(2),sprintf('%d ms',legPos(3)),'FontSize',fontSize,'horizontalAlignment','Left','VerticalAlignment','Top');
text(legPos(1),legPos(2),[sprintf('%.1f',legPos(4)),' \DeltaG/R'],'FontSize',fontSize,'horizontalAlignment','Left','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');






























